import { Object3D, Vector3 } from "three";
import { IEditorHistoryOptions } from "../EditorHistory";
import EditorCommand, { EnumEditorCommandName, EnumEditorCommandType, IEditorCommandOutput } from "./EditorCommands";
export interface IEditorSetPostionCommandsOptions extends IEditorHistoryOptions {
    object: Object3D,
    newPosition: Vector3,
    oldPosition: Vector3
}
export const EVENT_EDITOR_SET_POSTION_COMMANDS = "EVENT_EDITOR_SET_POSTION_COMMANDS"
export default class EditorSetPostionCommands extends EditorCommand {
    object: Object3D;
    newPosition: Vector3 = new Vector3();
    oldPosition: Vector3 = new Vector3();
    constructor(options: IEditorSetPostionCommandsOptions) {
        super(options);
        let { object, newPosition, oldPosition } = options;
        this.type = EnumEditorCommandType.SetPositionCommand;
        this.name = EnumEditorCommandName.SetPosition;
        this.object = options.object;
        if (object !== undefined && newPosition !== undefined) {
            this.oldPosition = object.position.clone();
            this.newPosition = newPosition.clone();
        }
        if (oldPosition !== undefined) {
            this.oldPosition = oldPosition.clone();
        }
    }
    execute() {
        this.object.position.copy(this.newPosition);
        this.object.updateMatrixWorld(true);
        this.editor.$emit(EVENT_EDITOR_SET_POSTION_COMMANDS, this.newPosition)
    }
    undo() {
        this.object.position.copy(this.oldPosition);
        this.object.updateMatrixWorld(true);
        this.editor.$emit(EVENT_EDITOR_SET_POSTION_COMMANDS, this.oldPosition)
    }
    update(command: EditorSetPostionCommands) {
        this.newPosition.copy(command.newPosition);
    }

    toJSON() {
        const output: IEditorCommandOutput = {
            ...super.toJSON(),
            uuid: this.object.uuid,
            oldPosition: this.oldPosition.toArray(),
            newPosition: this.newPosition.toArray()
        };
        return output;
    }

    fromJSON(json: IEditorCommandOutput) {
        super.fromJSON(json);
        let object = this.editor.objectByUuid(json.uuid!);
        if (object) {
            this.object = object;
            this.oldPosition = new Vector3().fromArray(json.oldPosition!);
            this.newPosition = new Vector3().fromArray(json.newPosition!);
        } else {
            throw new Error('找不到uuid对应的对象')
        }
    }

}